//
//  UploadImageViewModel.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/29.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import HandyJSON

class UploadImageViewModel: NSObject {

    func uploadImages(_ images: [String]) -> Observable<[String]> {
        return Observable<[String]>.create { (observer) -> Disposable in
            var idArray: [String] = []
            let disposeBag = Observable.from(images.enumerated())
                .concatMap { (index, url) -> Observable<String> in
                    return UpLoadApimanager.uploadFile(URL.init(fileURLWithPath: url))
                        .request()
                        .dataModelMap(UploadModel.self)
                        .map{ $0.id }
            }.subscribe(onNext: { (id) in
                idArray.append(id)
            }, onError: { (error) in
                observer.onError(error)
                observer.onCompleted()
            }, onCompleted: {
                observer.onNext(idArray)
                observer.onCompleted()
            })
            return Disposables.create([disposeBag])
        }.trackingLoading("上传图片中".localized)
    }
    
}

struct UploadModel: HandyJSON {
    var id = ""
}
